VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr(

by Anon (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Fri 17th August 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr(" << hello >>

Rate Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr(



                      ByVal sSubStart As String, _
                      ByVal sSubEnd As String, _
                      ByVal iOptComp As Integer) As String

    If (Len(s) = 0 Or Len(sSubStart) = 0 Or Len(sSubEnd) = 0) Then
        GoTo end_rem_sub_str
    End If

    Dim instrS As Long, instrE As Long
    Dim begin  As Long, p  As Long, lastP As Long
    Dim lS As Long, lE As Long
    
    lS = Len(sSubStart)
    lE = Len(sSubEnd)

    begin = 1

    Do

        instrS = InStr(begin, s, sSubStart, iOptComp)
        instrE = InStr(instrS + lS, s, sSubEnd, iOptComp)

        If ((instrS <= 0) Or (instrE <= 0)) Then Exit Do
            
        p = instrS

        Do
            lastP = p
            p = InStr(lastP + lS, s, sSubStart, iOptComp)
            If (p = 0) Or (p >= instrE) Then Exit Do
        Loop

        s = Left$(s, lastP - 1) & Mid$(s, instrE + lE)
        begin = lastP
        
    Loop
    
end_rem_sub_str:

    RemoveSubStr = s

End Function

Sub test_RemSubStr()

    Dim sTmp As String

    sTmp = ""
    sTmp = sTmp & "<html><body><!-- content begin -->" & vbCr
    sTmp = sTmp & "<SCRIPT language=""JavaScript1.2"">" & vbCr
    sTmp = sTmp & Chr$(9) & "document.write('<font face=""Courier"">');" & vbCr
    sTmp = sTmp & Chr$(9) & "document.writeln('<font face=""Courier"">');" & vbCr
    sTmp = sTmp & "</SCRIPT>" & vbCr
    sTmp = sTmp & "<< <b>Hello!</b> >>" & vbCr
    sTmp = sTmp & "<script language=""JavaScript1.2"">" & vbCr
    sTmp = sTmp & Chr$(9) & "document.write('</font>');" & vbCr
    sTmp = sTmp & Chr$(9) & "document.writeln('</font>');" & vbCr
    sTmp = sTmp & "</script>" & vbCr
    sTmp = sTmp & "<!-- content end --></body></html>"
    
    MsgBox sTmp, 0, "Remove SubString: Original string"
    
    sTmp = RemoveSubStr(sTmp, "<script", "</script>", vbTextCompare)
    
    MsgBox sTmp, 0, "Remove SubString: Scripts removed"
    
    sTmp = RemoveSubStr(sTmp, "<", ">", vbCompareBinary)
    
    MsgBox sTmp, 0, "Remove SubString: All tags removed"

End Sub

Download this snippet    Add to My Saved Code

Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr( Comments

No comments have been posted about Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr(. Why not be the first to post a comment about Removes all occurrences (case-sensitive) of a substring within another string: RemoveSubstr(.

Post your comment

Subject:
Message:
0/1000 characters